<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm :searchFunc="searchFunc" :resetFunc="onReset">
        <a-form-item label="" class="table-search-item">
          <JeepayDateRangePicker ref="dateRangePicker" v-model:value="vdata.searchData['queryDateRange']" customDateRangeType="dateTime" />
        </a-form-item>
        <JeepaySearchInfoInput v-model:value="vdata.searchData['mchNo']" placeholder="商户号" :textUpStyle="true" :mchNoAndName="true" showType="MCH" />

        <jeepay-text-up v-model:value="vdata.searchData.mbrTel" :placeholder="'会员手机号'" />
        <jeepay-text-up v-model:value="vdata.searchData.mbrName" :placeholder="'会员名称'" />
        <jeepay-text-up v-model:value="vdata.searchData.relaBizOrderId" :placeholder="'关联订单号'" />
        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData.bizType" placeholder="业务类型">
            <a-select-option value="">全部类型</a-select-option>
            <a-select-option value="1">支付充值</a-select-option>
            <a-select-option value="2">现金充值</a-select-option>
            <a-select-option value="3">会员消费</a-select-option>
            <a-select-option value="4">消费退款</a-select-option>
            <a-select-option value="5">人工调账</a-select-option>
          </a-select>
        </a-form-item>
      </JeepaySearchForm>

      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :initData="true"
        :reqTableDataFunc="reqTableDataFunc"
        :tableColumns="tableColumns"
        :searchData="vdata.searchData"
        :statisticsIsShow="$access('ENT_MEMBER_ACCOUNT_HISTORY_LIST')"
        rowKey="hid"
        @btnLoadClose="vdata.btnLoading=false"
      >
        <template #statistics>
          <div class="statistics-list">
            <div v-for="(item, index) in countList" :key="index" class="item">
              <div v-if="item.type == 'line'" class="line" />
              <div class="title">{{ item.title }}</div>
              <div v-if="item.title" class="amount" :style="{color: item.textColor}">
                <span class="amount-num">{{ item.content }}</span>
                <span v-if="item.isAmount">元</span>
              </div>
            </div>
          </div>
        </template>
        <template #topBtnSlot />
        <template #bodyCell="{column,record}">
          <template v-if="column.key == 'beforeAmount'">
            <b>￥{{ record.beforeAmount / 100 }}</b>
          </template>
          <template v-if="column.key == 'changeAmount'">
            <b>￥{{ record.changeAmount / 100 }}</b>
          </template>
          <template v-if="column.key == 'afterAmount'">
            <b>￥{{ record.afterAmount / 100 }}</b>
          </template>

          <template v-if="column.key == 'bizType'">
            {{ record.bizType === 1?'支付充值':record.bizType === 2?'现金充值':record.bizType === 3?'会员消费':record.bizType === 4?'消费退款':record.bizType === 5?'人工调账':'其他' }}
          </template>

          <template v-if="column.key == 'op'">
            <!-- 操作列插槽 -->
            <JeepayTableColumns>
              <a-button v-if="$access('ENT_MEMBER_ACCOUNT_HISTORY_VIEW')" type="link" @click="detailFunc(record.hid)">详情</a-button>
            </JeepayTableColumns>
          </template>
        </template>
      </JeepayTable>
    </a-card>
    <!-- 新增  -->
    <Detail ref="detail" :callbackFunc="searchFunc" />
  </page-header-wrapper>
</template>

<script lang="ts" setup>
import { API_URL_MEMBER_ACCOUNT_HISTORY_LIST, req, $memberAccountHistoryCount } from '@/api/manage'
import Detail from './Detail.vue'
import {ref, onMounted, reactive, getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'

const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties

// eslint-disable-next-line no-unused-vars
const tableColumns = reactive([
  { key: 'hid', fixed: 'left', title: '流水ID', dataIndex: 'hid' },
  { key: 'relaBizOrderId', title: '关联订单号', dataIndex: 'relaBizOrderId' },
  // { key: 'mbrId', title: '会员ID', dataIndex: 'mbrId' },
  { key: 'mchNo', title: '商户号', dataIndex: 'mchNo' },
  { key: 'mbrTel', title: '手机号', dataIndex: 'mbrTel' },
  { key: 'mbrName', title: '会员名称', dataIndex: 'mbrName' },
  { key: 'beforeAmount', title: '变动前余额', dataIndex: 'beforeAmount' },
  { key: 'changeAmount', title: '变动金额', dataIndex: 'changeAmount' },
  { key: 'afterAmount', title: '变动后余额', dataIndex: 'afterAmount' },
  { key: 'bizType', title: '业务类型' },
  { key: 'createdAt', title: '创建日期', dataIndex: 'createdAt' },
  { key: 'op', title: '操作', fixed: 'right', align: 'center' }
])

  const detail = ref()
  const infoTable = ref()
  const dateRangePicker = ref()
  let countList:any = ref([]) // 数据统计数组

  onMounted(()=>{
    vdata.searchData['mbrId'] = useRoute().query.mbrId
      queryFunc()
      getCount()
  })

  // 数据统计
  const getCount = () => {
    $memberAccountHistoryCount(vdata.searchData).then( res => {
      countList.value = [ 
        {title: '变动条数', content: res.countNum, isAmount: false},
        {type: 'line'},
        {title: '变动金额', textColor: '#1A66FF', isAmount: true, content: (Math.abs(res.changeAmount) / 100).toFixed(2)},
      ]
    })
  }

  const queryFunc = () => {
    
    vdata.btnLoading = true
    infoTable.value.refTable(true)
  }

  const vdata = reactive({
    btnLoading: false,
    tableColumns: tableColumns,
    searchData: {} as any
  })

  // 请求table接口数据
  function reqTableDataFunc(params){
    return req.list(API_URL_MEMBER_ACCOUNT_HISTORY_LIST, params)
  }

  function searchFunc () { // 点击【查询】按钮点击事件
    getCount()
    infoTable.value.refTable(true)
  }

  function detailFunc (recordId) { // 业务通用【详情】 函数
    detail.value.show(recordId)
  }

  function onReset(){
    //重置搜索内容
    dateRangePicker.value.returnSelectModel()
    vdata.searchData = { queryDateRange: '' }
  }
 
</script>

<style lang="less" scoped>
</style>
